'/
' Fetherquest Online
' 
' =========================================================================
' 
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
' 
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
' 
' You should have received a copy of the GNU General Public License
' along with this program.  If not, see <http://www.gnu.org/licenses/>.
'/

type InputManager extends Base

	'// -------------------------------------------------------------------------------------------------------------
	'// The 'console' key. It's missing from the "keycodes" framework.
	'// -------------------------------------------------------------------------------------------------------------
	global KEY_CONSOLE:short = 167;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Initialize the input.
	'// -------------------------------------------------------------------------------------------------------------
	method init()
	
		sConsoleManager.writeLog("Initalizing input.");
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Handle the keyboard input.
	'// -------------------------------------------------------------------------------------------------------------
	method handleKeyboard()
	
		'// Toggle fullscreen.
		if ((keyDown(KEY_LALT) or keyDown(KEY_RALT)) and keyDown(KEY_ENTER)) then
		
			if (not sMaster.p_fullscreen) ..
				sConsoleManager.execCommand("_r_fullscreenmode"); ..
			else if (sMaster.p_fullscreen) ..
				sConsoleManager.execCommand("_r_windowmode");
			
			sConsoleManager.execCommand("_vid_restart");
		
		end if
		
		'// Get the current keystroke.
		local currentKeyStroke:short = 0;
		
		if (not m_deactive) ..
			currentKeyStroke = getCurrentKey(); ..
		else ..
			currentKeyStroke = GUI_InputThread.m_lastKey;
		
		'// If the console is visible, use the input as writing instead.
		if (sConsoleManager.isVisible()) then
		
			'// Hide the console if escape or the console key is pressed.
			if (keyDown(KEY_ESCAPE) or currentKeyStroke = KEY_CONSOLE) ..
				sConsoleManager.toggle();
			
			sConsoleManager.handleInput(currentKeyStroke);
			
			return;
		
		end if
		
		'// Console.
		if (currentKeyStroke = KEY_CONSOLE) ..
			sConsoleManager.toggle();
		
		'// Screen dependent input.
		select (sVideo.m_curScreen)
		
			case sVideo.SCR_MENU
				'///- Handle input in the main menu.
			case sVideo.SCR_GAME_LOOP
				'///- Handle input in-game.
		
		end select
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Handle the mouse input.
	'// -------------------------------------------------------------------------------------------------------------
	method handleMouse()
	
		'// If the console is visible, ignore the mouse.
		if (sConsoleManager.m_posY > 0) ..
			return;
		
		'// Screen dependent input.
		select (sVideo.m_curScreen)
		
			case sVideo.SCR_MENU
				'///- Handle input in the main menu.
			case sVideo.SCR_GAME_LOOP
				'///- Handle input in-game.
		
		end select
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Get the currently pressed key.
	'// -------------------------------------------------------------------------------------------------------------
	method getCurrentKey:short()
	
		return GUI_InputThread.getCurrentKey();
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Activates the input.
	'// -------------------------------------------------------------------------------------------------------------
	function activate()
	
		m_deactive = false;
	
	end function
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Deactivates the input temporarily.
	'// -------------------------------------------------------------------------------------------------------------
	function deactivate()
	
		m_deactive = true;
	
	end function
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Set this to true to deactivate the input temporarily (handy when input should only be given to the GUI,
	'// for example).
	'// -------------------------------------------------------------------------------------------------------------
	global m_deactive:byte = true;

end type

global sInputManager:InputManager = new InputManager;
